package com.eostoken.eos_wallet.ui.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CompoundButton;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.ui.fragmentfactory.FragmentFactory;
import com.eostoken.eos_wallet.utils.Constants;
import com.eostoken.eos_wallet.utils.TextviewUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import butterknife.BindView;
import butterknife.OnClick;

import static com.eostoken.eos_wallet.utils.Constants.ADD_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.BACKUP_MNEMONIC;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_NAME;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_HINT;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET_PASSWORD;

public class CreateAccountFragment extends BaseFragment {


    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.createWalletExplainTv)
    AppCompatTextView createWalletExplainTv;
    @BindView(R.id.accountNameEt)
    AppCompatEditText accountNameEt;
    @BindView(R.id.passwordEt)
    AppCompatEditText passwordEt;
    @BindView(R.id.confirmPasswordEt)
    AppCompatEditText confirmPasswordEt;
    @BindView(R.id.promptMessageEt)
    AppCompatEditText promptMessageEt;
    @BindView(R.id.clauseCheckBox)
    AppCompatCheckBox clauseCheckBox;
    @BindView(R.id.stateTv)
    AppCompatTextView stateTv;

    private boolean isAgree = false;
    private String accountName;
    private String password;
    private String hint;
    private boolean isPass = false;

//    private static class LazyHolder {
//        private static final CreateAccountFragment INSTANCE = new CreateAccountFragment();
//    }
//
//    public static final CreateAccountFragment getInstance() {
//        return LazyHolder.INSTANCE;
//    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_create_account;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        QMUIStatusBarHelper.setStatusBarDarkMode(getActivity());
        BarUtils.setStatusBarColor(getActivity(), ColorUtils.getColor(R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(getActivity(), false);
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_create_wallet));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getFragmentManager().getBackStackEntryCount() < 1) {
                    finish();
                } else {
                    FragmentUtils.pop(getFragmentManager());
                }
            }
        });
        clauseCheckBox.setText(Html.fromHtml(StringUtils.getString(R.string.str_agreement_clause_2)));

        clauseCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isAgree = isChecked;
            }
        });

        createWalletExplainTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                createWalletExplainTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                final String newText = TextviewUtils.autoSplitText(createWalletExplainTv, ".");
                if (!TextUtils.isEmpty(newText)) {
                    createWalletExplainTv.setText(newText);
                }
            }
        });

        accountNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                accountName = s.toString();
                boolean isPass = RegexUtils.isMatch(Constants.REGEX_ACCOUNT_NAME, accountName);
                if (!isPass) {
                    stateTv.setText(StringUtils.getString(R.string.str_invalid_accountname));
                    stateTv.setTextColor(ColorUtils.getColor(R.color.qmui_config_color_red));
                    isPass = false;
                } else {
                    getVerify(accountName);
                }
            }
        });
    }

    private void getVerify(String accountname) {
        WalletUtils.getVerifyWallet(Utils.getApp(), accountname, new CommonCallback() {
            @Override
            public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                stateTv.setText(StringUtils.getString(R.string.str_invalid_accountname));
                stateTv.setTextColor(ColorUtils.getColor(R.color.qmui_config_color_red));
                isPass = false;
            }

            @Override
            public void onFailure(String msg, int responseFailCode) {
                stateTv.setText(StringUtils.getString(R.string.str_valid_accountname));
                stateTv.setTextColor(ColorUtils.getColor(R.color.app_color_blue));
                isPass = true;
            }
        });
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @OnClick(R.id.createWalletBtn)
    public void onViewClicked() {
        accountName = accountNameEt.getText().toString().trim();
        password = passwordEt.getText().toString().trim();
        String confirmPassword = confirmPasswordEt.getText().toString().trim();
        if (!isPass) {
            ToastUtils.showLong(R.string.str_please_input_accountname_rule);
            return;
        }
        if (ObjectUtils.isEmpty(password)) {
            ToastUtils.showLong(R.string.str_input_password);
            return;
        }
        if (password.getBytes().length < 6) {
            ToastUtils.showLong(R.string.str_hint_password);
            return;
        }
        if (!ObjectUtils.equals(password, confirmPassword)) {
            ToastUtils.showLong(R.string.str_inconformity_password);
            return;
        }
        if (!isAgree) {
            ToastUtils.showLong(R.string.str_protocol_not_checked);
            return;
        }
        hint = promptMessageEt.getText().toString().trim();
//        BackupMnemonicFragment fragment = new BackupMnemonicFragment();
        Fragment fragment = FragmentFactory.getInstatic().getFragment(BACKUP_MNEMONIC);
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ACCOUNT_NAME, accountName);
        bundle.putString(EXTRA_WALLET_PASSWORD, password);
        bundle.putString(EXTRA_HINT, hint);
        fragment.setArguments(bundle);
        FragmentUtils.replace(getFragmentManager(), fragment, R.id.frag_container, "BackupMnemonicFragment", true);
    }
}
